package com.shsh.safemanage.action;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.shsh.safemanage.dao.DaoCreator;
import com.shsh.safemanage.dao.DepartmentDao;
import com.shsh.safemanage.dao.PostCheckDao;
import com.shsh.safemanage.dao.ProblemDao;
import com.shsh.safemanage.dao.TitlePostManageGroupDao;
import com.shsh.safemanage.dao.UserDao;
import com.shsh.safemanage.model.Admin;
import com.shsh.safemanage.model.Department1PostcheckMapping;
import com.shsh.safemanage.model.Department2;
import com.shsh.safemanage.model.Department3;
import com.shsh.safemanage.model.User;
import com.shsh.safemanage.util.Constants;
import com.shsh.safemanage.util.RequestUtil;
import com.shsh.safemanage.util.Util;

public class AdminAction {
	public boolean login(HttpServletRequest request) {
		String adminName = request.getParameter(Constants.REQUEST_PARAMETER_ADMIN_NAME);
		String password = request.getParameter(Constants.REQUEST_PARAMETER_PASSWORD);

		UserDao userDao = DaoCreator.createUserDao();
		Admin adminUser = userDao.adminLogin(adminName, password);
		if (adminUser == null) {
			request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR, Constants.ERROR_KEY_ADMIN_LOGIN_FAILD);
		} else {
			HttpSession session = request.getSession();
			session.setAttribute(Constants.SESSION_ADMIN_USER, adminUser);
		}
		return adminUser != null;
	}

	public boolean updateDepartment3Role(HttpServletRequest request) {
		String depart3IDStr = request.getParameter(Constants.REQUEST_PARAMETER_DEPARTMENT3ID);
		String roleIDStr = request.getParameter(Constants.REQUEST_PARAMETER_ROLE_ID);

		Long depart3ID = 0L;
		Long roleID = 0L;

		try {
			depart3ID = Long.parseLong(depart3IDStr);
		} catch (Exception e) {
			request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR, Constants.ERROR_KEY_ADMIN_ILLEGAL_DEPARTMENT3_ID);
			return false;
		}
		try {
			roleID = Long.parseLong(roleIDStr);
		} catch (Exception e) {
			request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR, Constants.ERROR_KEY_ADMIN_ILLEGAL_ROLE_ID);
			return false;
		}

		DepartmentDao departmentDao = DaoCreator.createDepartmentDao();

		boolean isSuccess = departmentDao.updateDepartment3andRoleMapping(depart3ID, roleID);

		return isSuccess;
	}

	public boolean updateJobpostRole(HttpServletRequest request) {
		String jobPostIDStr = request.getParameter(Constants.REQUEST_PARAMETER_JOB_POST_ID);
		String roleIDStr = request.getParameter(Constants.REQUEST_PARAMETER_ROLE_ID);

		Long jobPostID = 0L;
		Long roleID = 0L;

		try {
			jobPostID = Long.parseLong(jobPostIDStr);
		} catch (Exception e) {
			request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR, Constants.ERROR_KEY_ADMIN_ILLEGAL_DEPARTMENT3_ID);
			return false;
		}
		try {
			roleID = Long.parseLong(roleIDStr);
		} catch (Exception e) {
			request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR, Constants.ERROR_KEY_ADMIN_ILLEGAL_ROLE_ID);
			return false;
		}

		TitlePostManageGroupDao jobPostDao = DaoCreator.createTitlePostManageGroupDao();

		boolean isSuccess = jobPostDao.updateJobPostandRoleMapping(jobPostID, roleID);

		return isSuccess;
	}

	public boolean updateUserOpenRegister(HttpServletRequest request) {
		String userIDStr = request.getParameter(Constants.REQUEST_PARAMETER_USER_ID);

		Long userID = 0L;

		try {
			userID = Long.parseLong(userIDStr);
		} catch (Exception e) {
			request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR, Constants.ERROR_KEY_ADMIN_ILLEGAL_DEPARTMENT3_ID);
			return false;
		}

		UserDao userDao = DaoCreator.createUserDao();

		boolean isSuccess = userDao.updateUserOpenRegister(userID);

		return isSuccess;
	}

	public boolean deleteProblems(HttpServletRequest request) {
		String problemIDsStr = request.getParameter(Constants.REQUEST_PARAMETER_PROBLEM_IDS);

		List<Long> problemIDList = new ArrayList<Long>();
		String[] problemIDStrs = null;

		try {
			problemIDStrs = problemIDsStr.split(",");
			Long problemID = 0L;
			for (String problemIDStr : problemIDStrs) {
				problemID = Long.parseLong(problemIDStr);
				problemIDList.add(problemID);
			}
		} catch (Exception e) {
			request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR, Constants.ERROR_KEY_ADMIN_ILLEGAL_DEPARTMENT3_ID);
			return false;
		}

		ProblemDao problemDao = DaoCreator.createProblemDao();

		boolean isSuccess = problemDao.deleteProblemByIDList(problemIDList.toArray(new Long[problemIDList.size()]));
		if (!isSuccess) {
			request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR, Constants.ERROR_KEY_ADMIN_DELETE_PROBLEM_FAIL);
		}

		return isSuccess;
	}

	public boolean getRegisteredUsers(HttpServletRequest request) {
		UserDao userDao = DaoCreator.createUserDao();

		Integer pageNum = RequestUtil.getParameter(request, Constants.REQUEST_PARAMETER_PAGE_NUM, 1);

		Map<String, Object> daoMap = userDao.getUsersByIsRegistered(pageNum, true);
		request.setAttribute(Constants.DAO_RESULT, daoMap.get(Constants.DAO_RESULT));
		request.setAttribute(Constants.DAO_RESULT_NUMBER, daoMap.get(Constants.DAO_RESULT_NUMBER));
		return true;
	}

	public boolean deleteUserById(HttpServletRequest request) {
		String userIDStr = (String) request.getParameter(Constants.REQUEST_PARAMETER_USER_ID);
		Long userID = 0l;
		try {
			userID = Long.parseLong(userIDStr);
		} catch (Exception e) {

		}

		UserDao userDao = DaoCreator.createUserDao();
		userDao.deleteUserById(userID);

		return true;
	}

	public boolean addPostcheckStandard(HttpServletRequest request) {
		Long postcheckID = 0l;
		Long postcheckPatternID = 0l;
		Long postcheckContentID = 0l;

		try {
			postcheckID = Long.parseLong((String) request.getParameter(Constants.REQUEST_PARAMETER_POSTCHECK_ID));
			postcheckPatternID = Long.parseLong((String) request.getParameter(Constants.REQUEST_PARAMETER_POSTCHECK_PATTERN_ID));
			postcheckContentID = Long.parseLong((String) request.getParameter(Constants.REQUEST_PARAMETER_POSTCHECK_CONTENT_ID));
		} catch (Exception e) {

		}

		String standard = (String) request.getParameter(Constants.REQUEST_PARAMETER_POSTCHECK_STANDARD);
		String sources = (String) request.getParameter(Constants.REQUEST_PARAMETER_POSTCHECK_SOURCES);
		if (sources != null && sources.length() > 0) {
			sources = sources.substring(1);// delete the first ","
		}
		String gradeStandard = (String) request.getParameter(Constants.REQUEST_PARAMETER_POSTCHECK_GRADE_STANDARD);
		String checkFrequency = (String) request.getParameter(Constants.REQUEST_PARAMETER_POSTCHECK_CHECK_FREQUENCY);

		PostCheckDao postcheckDao = DaoCreator.createPostCheckDao();

		postcheckDao.addPostcheckStandard(postcheckID, postcheckPatternID, postcheckContentID, standard, sources, gradeStandard,
				checkFrequency);

		return true;
	}

	public boolean updatePostcheckStandard(HttpServletRequest request) {
		Long postcheckID = 0l;
		Long postcheckPatternID = 0l;
		Long postcheckContentID = 0l;
		Long postcheckStandardID = 0l;

		try {
			postcheckID = Long.parseLong((String) request.getParameter(Constants.REQUEST_PARAMETER_POSTCHECK_ID));
			postcheckPatternID = Long.parseLong((String) request.getParameter(Constants.REQUEST_PARAMETER_POSTCHECK_PATTERN_ID));
			postcheckContentID = Long.parseLong((String) request.getParameter(Constants.REQUEST_PARAMETER_POSTCHECK_CONTENT_ID));
			postcheckStandardID = Long.parseLong((String) request.getParameter(Constants.REQUEST_PARAMETER_POSTCHECK_STANDARD_ID));
		} catch (Exception e) {

		}

		String standard = (String) request.getParameter(Constants.REQUEST_PARAMETER_POSTCHECK_STANDARD);
		String sources = (String) request.getParameter(Constants.REQUEST_PARAMETER_POSTCHECK_SOURCES);
		if (sources != null && sources.length() > 0) {
			sources = sources.substring(1);// delete the first ","
		}
		String gradeStandard = (String) request.getParameter(Constants.REQUEST_PARAMETER_POSTCHECK_GRADE_STANDARD);
		String checkFrequency = (String) request.getParameter(Constants.REQUEST_PARAMETER_POSTCHECK_CHECK_FREQUENCY);

		PostCheckDao postcheckDao = DaoCreator.createPostCheckDao();

		postcheckDao.updatePostcheckStandard(postcheckID, postcheckPatternID, postcheckContentID, postcheckStandardID, standard, sources,
				gradeStandard, checkFrequency);

		return true;
	}

	public boolean deletePostcheckStandard(HttpServletRequest request) {
		Long postcheckStandardID = 0l;
		try {
			postcheckStandardID = Long.parseLong((String) request.getParameter(Constants.REQUEST_PARAMETER_POSTCHECK_STANDARD_ID));
		} catch (Exception e) {
			return false;
		}

		PostCheckDao postcheckDao = DaoCreator.createPostCheckDao();
		return postcheckDao.deletePostcheckStandardByID(postcheckStandardID);
	}

	public boolean addDepartment1PostcheckMapping(HttpServletRequest request) {
		String depart1IDStr = request.getParameter(Constants.REQUEST_PARAMETER_DEPARTMENT1ID);
		String postcheckIDStr = request.getParameter(Constants.REQUEST_PARAMETER_POSTCHECK_ID);

		Long depart1ID = null;
		try {
			depart1ID = Long.parseLong(depart1IDStr);
		} catch (Exception e) {
			request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR, Constants.ERROR_KEY_ADMIN_ILLEGAL_DEPARTMENT3_ID);
			return false;
		}

		Long postcheckID = null;
		try {
			postcheckID = Long.parseLong(postcheckIDStr);
		} catch (Exception e) {
			request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR, Constants.ERROR_KEY_ADMIN_ILLEGAL_POSTCHECK_ID);
			return false;
		}

		PostCheckDao postcheckDao = DaoCreator.createPostCheckDao();

		List<Department1PostcheckMapping> lst = postcheckDao.searchDepartment1PostcheckMapping(depart1ID, postcheckID);

		if (lst.size() > 0) {
			request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR, Constants.ERROR_KEY_ADMIN_DEPARTMENT1_POSTCHECK_EXISTS);
			return false;
		}

		boolean isSuccess = postcheckDao.addDepartment1PostcheckMapping(depart1ID, postcheckID);

		this.clearRelateReport(request.getSession().getServletContext(), postcheckID);

		return true;
	}

	public boolean deleteDepartment1PostcheckMapping(HttpServletRequest request) {
		String depart1IDStr = request.getParameter(Constants.REQUEST_PARAMETER_DEPARTMENT1ID);
		String postcheckIDStr = request.getParameter(Constants.REQUEST_PARAMETER_POSTCHECK_ID);

		Long depart1ID = null;
		try {
			depart1ID = Long.parseLong(depart1IDStr);
		} catch (Exception e) {
			request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR, Constants.ERROR_KEY_ADMIN_ILLEGAL_DEPARTMENT3_ID);
			return false;
		}

		Long postcheckID = null;
		try {
			postcheckID = Long.parseLong(postcheckIDStr);
		} catch (Exception e) {
			request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR, Constants.ERROR_KEY_ADMIN_ILLEGAL_POSTCHECK_ID);
			return false;
		}

		PostCheckDao postcheckDao = DaoCreator.createPostCheckDao();

		boolean isSuccess = postcheckDao.deleteDepartment1PostcheckMapping(depart1ID, postcheckID);
		this.clearRelateReport(request.getSession().getServletContext(), postcheckID);

		return true;
	}

	public boolean deleteDepart3(HttpServletRequest request) {
		String depart3IDStr = request.getParameter(Constants.REQUEST_PARAMETER_DEPARTMENT3ID);

		Long depart3ID = 0L;

		try {
			depart3ID = Long.parseLong(depart3IDStr);
		} catch (Exception e) {
			request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR, Constants.ERROR_KEY_ADMIN_ILLEGAL_DEPARTMENT3_ID);
			return false;
		}

		DepartmentDao departmentDao = DaoCreator.createDepartmentDao();

		boolean isSuccess = departmentDao.deleteDepartment3(depart3ID);

		return isSuccess;
	}
	
	public boolean deleteDepart2(HttpServletRequest request) {
		String depart2IDStr = request.getParameter(Constants.REQUEST_PARAMETER_DEPARTMENT2ID);
		
		Long depart2ID = 0L;
		try {
			depart2ID = new Long(depart2IDStr);
		} catch (Exception e) {
			return false;
		}
		
		DepartmentDao departmentDao = DaoCreator.createDepartmentDao();
		boolean isSuccess = departmentDao.deleteDepartment2(depart2ID);
		
		return isSuccess;
	}

	public boolean addDepart2(HttpServletRequest request) {
		String depart1IDStr = request.getParameter(Constants.REQUEST_PARAMETER_DEPARTMENT1ID);
		String depart2Name = request.getParameter(Constants.REQUEST_PARAMETER_DEPARTMENT2_NAME);

		Long depart1ID = 0L;

		try {
			depart1ID = Long.parseLong(depart1IDStr);
		} catch (Exception e) {
			request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR, Constants.ERROR_KEY_ADMIN_ILLEGAL_DEPARTMENT3_ID);
			return false;
		}

		if (depart2Name != null) {
			depart2Name = Util.isoToUTF8(depart2Name);
		}

		DepartmentDao departmentDao = DaoCreator.createDepartmentDao();

		Department2 depart2 = new Department2();
		depart2.setDepartment1ID(depart1ID);
		depart2.setDepartment2Name(depart2Name);

		boolean isSuccess = departmentDao.addDepartment2(depart2);
		request.setAttribute(Constants.REQUEST_ATTRIBUTE_DEPARTMENT2_ID, depart2.getId());

		return isSuccess;
	}

	public boolean addDepart3(HttpServletRequest request) {
		String depart1IDStr = request.getParameter(Constants.REQUEST_PARAMETER_DEPARTMENT1ID);
		String depart2IDStr = request.getParameter(Constants.REQUEST_PARAMETER_DEPARTMENT2ID);
		String depart3Name = request.getParameter(Constants.REQUEST_PARAMETER_DEPARTMENT3_NAME);
		String roleIDStr = request.getParameter(Constants.REQUEST_PARAMETER_ROLE_ID);

		Long depart1ID = 0L;
		Long depart2ID = 0L;
		Long roleID = 0L;

		try {
			depart1ID = Long.parseLong(depart1IDStr);
		} catch (Exception e) {
			request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR, Constants.ERROR_KEY_ADMIN_ILLEGAL_DEPARTMENT3_ID);
			return false;
		}

		try {
			depart2ID = Long.parseLong(depart2IDStr);
		} catch (Exception e) {
			return false;
		}

		try {
			roleID = Long.parseLong(roleIDStr);
		} catch (Exception e) {
			return false;
		}

		if (depart3Name != null) {
			depart3Name = Util.isoToUTF8(depart3Name);
		}

		DepartmentDao departmentDao = DaoCreator.createDepartmentDao();

		Department3 depart3 = new Department3();
		depart3.setDepartment1ID(depart1ID);
		depart3.setDepartment2ID(depart2ID);
		depart3.setDepartment3Name(depart3Name);
		depart3.setRoleID(roleID);

		boolean isSuccess = departmentDao.addDepartment3(depart3);

		return isSuccess;
	}

	public boolean browseDepartment1PostcheckMapping(HttpServletRequest request) {
		String depart1IDStr = request.getParameter(Constants.REQUEST_PARAMETER_DEPARTMENT1ID);
		String postcheckIDStr = request.getParameter(Constants.REQUEST_PARAMETER_POSTCHECK_ID);

		Long depart1ID = null;
		try {
			depart1ID = Long.parseLong(depart1IDStr);
		} catch (Exception e) {
		}

		Long postcheckID = null;
		try {
			postcheckID = Long.parseLong(postcheckIDStr);
		} catch (Exception e) {
		}

		PostCheckDao postcheckDao = DaoCreator.createPostCheckDao();

		List<Department1PostcheckMapping> lst = postcheckDao.searchDepartment1PostcheckMapping(depart1ID, postcheckID);

		request.setAttribute(Constants.REQUEST_ATTRIBUTE_DEPARTMENT1_POSTCHECK_MAPPING_LIST, lst);
		return true;
	}

	private void clearRelateReport(ServletContext application, Long postcheckID) {

		Enumeration attributeKeys = application.getAttributeNames();

		while (attributeKeys.hasMoreElements()) {
			Object obj = attributeKeys.nextElement();
			String key = (String) obj;

			String regx = "^" + Constants.APPLICATION_POSTCHECK_SUMMARY_REPORT_TABLE + "_" + postcheckID + "_\\d+_\\d+$";
			if (Pattern.matches(regx, key)) {
				application.removeAttribute(key);
			}
		}
	}
}
